package com.rain.activity.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rain.activity.R;

import java.util.List;
import java.util.Map;

/**
 * Created by Rain on 2016/4/21.
 */
public class QuestionAdapter extends BaseAdapter {
    private Context ctx;
    private List<Map<String,Object>> values;

    public QuestionAdapter(Context ctx, List<Map<String, Object>> values) {
        this.ctx = ctx;
        this.values = values;
    }

    @Override
    public int getCount() {
        return values.size();
    }

    @Override
    public Object getItem(int position) {
        return values.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
           convertView = LayoutInflater.from(ctx).inflate(R.layout.list_question,null);
        }
        else {
            LinearLayout linearLayout = (LinearLayout) convertView;
            if(linearLayout.getChildCount()==2){
                linearLayout.removeViewAt(1);
            }
        }
        Map<String,Object> map = values.get(position);
        TextView question = (TextView) convertView.findViewById(R.id.list_question);
        question.setText(map.get("que").toString());

        boolean showflag = (boolean) map.get("showflag");
        if (showflag){
            TextView tv = new TextView(ctx);
            tv.setText(map.get("ans").toString());
            tv.setTextSize(14);
            tv.setTextColor(Color.RED);
            LinearLayout linearLayout = (LinearLayout) convertView;
            linearLayout.addView(tv);
        }
        return convertView;
    }
}
